class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :tickets, :dependent => :destroy
  validates_length_of :name, :within => 2..100
  validates_uniqueness_of :name

  serialize :emails
  attr_protected :emails

  def emails_field
     (self.emails && self.emails != [] && self.emails != [""]) ? self.emails * "; " : ""
  end
  
  def emails_field=(ef)
    self.emails = ef.delete(" ").split(';')
  end


  
  protected
  def validate
    emails.each{|email|
      errors.add(:emails, "are not correct") if (emails != [""]) && !( email =~  /(\w|.|_|-)+@(\w|.|_|-)+.([a-z]|[A-Z])+/ )
    }
  end
  
end
